package com.hang.controller;

import com.hang.common.BizException;
import com.hang.common.Result;
import com.hang.common.ResultCodeEnum;
import com.hang.model.Clazz;
import com.hang.model.Task;
import com.hang.service.*;
import com.hang.vo.LeavesDTO;
import com.hang.vo.TaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "教师端接口")
@RequestMapping(value = "teacher")
@RequiredArgsConstructor
public class TeacherController {
    private final TeacherService teacherService;

    private final ClazzService clazzService;

    private final TaskService taskService;

    private final AttendanceService attendanceService;

    private final LeaveService leaveService;

    @GetMapping("info/getAllClazz/{teacherId}/{page}/{size}")
    @ApiOperation(value = "分页获取班级列表")
    public Result getClazzListByTeacher(
            @PathVariable("teacherId") String teacherId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        List<Clazz> clazzList = teacherService.getHisList(teacherId,page,size);
        //判断是否还有数据
        if(teacherService.hasMoreData(teacherId,page,size)){
            return Result.ok(clazzList,Boolean.TRUE);
        }
        return Result.ok(clazzList,Boolean.FALSE);
    }

    @GetMapping("info/getClazzByID/{clazzId}")
    @ApiOperation(value = "获取班级的详尽信息")
    public Result getClazzInfoDetail(
        @PathVariable("clazzId") String clazzId
    ){
        //先获取班级的详尽信息
        Map<String,Object> map = clazzService.detail(clazzId);
        return Result.ok(map);
    }

    @PostMapping("task/createTask")
    @ApiOperation(value = "发布任务接口")
    public Result giveNewTask(@RequestBody TaskVo taskVo){
        try {
            taskService.saveTask(taskVo);
        }catch (BizException be){
            return Result.fail(ResultCodeEnum.POST_TASK_FAIL).message("发布任务失败");
        }
        return Result.ok();
    }

    @GetMapping("task/getTaskList/{clazzId}/{page}/{size}")
    @ApiOperation(value = "查看班级的任务列表")
    public Result getTaskByClazzId(
            @PathVariable("clazzId") String clazzId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        List<Task> tasks = taskService.getTaskLists(clazzId,page,size);

        //判断是否还有数据
        if(taskService.hasMoreData(clazzId,page,size)){
            return Result.ok(tasks,Boolean.TRUE);
        }
        return Result.ok(tasks,Boolean.FALSE);
    }

    @GetMapping("info/getResult/{taskId}/{status}")
    @ApiOperation(value = "查看任务对应的考勤结果")
    public Result getTaskDetail(
            @PathVariable("taskId") Long taskId,
            @PathVariable("status") Integer status
    ){
        Map<String,Object> map = taskService.getDetails(taskId,status);
        return Result.ok(map);
    }

    @GetMapping("getAllLaves/{teacherId}/{page}/{size}")
    @ApiOperation(value = "查看该老师管理的所有请假申请")
    public Result getAllLaves(
        @PathVariable("teacherId") String teacherId,
        @PathVariable("page") Integer page,
        @PathVariable("size") Integer size
    ){
        List<LeavesDTO> leaves = leaveService.getAllLeaves(teacherId,page,size);

        //判断是否还有数据
        if(leaveService.hasMoreData(teacherId,page,size)){
            return Result.ok(leaves,Boolean.TRUE);
        }
        return Result.ok(leaves,Boolean.FALSE);
    }

    @GetMapping("getLeaveById/{id}")
    @ApiOperation(value = "查看具体的请假申请")
    public Result getLeaveById(
            @PathVariable("id") Long id
    ){
        Map<String,Object> map = leaveService.detail(id);
        return Result.ok(map);
    }

    @PostMapping("approval/{id}/{status}")
    @ApiOperation(value = "请假审批")
    public Result approvalLeave(
            @PathVariable("id") Long id,
            @PathVariable("status") Integer status
    ){
        leaveService.approval(id,status);

        return Result.ok();
    }
}